﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmLoginMagnt : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        bool b登入Search, b登入Insert, b登入Edit, b登入Del;
        string oldpassword;
        public frmLoginMagnt(bool b登入Search, bool b登入Insert, bool b登入Edit, bool b登入Del)
        {
            InitializeComponent();
            this.b登入Search = b登入Search;
            this.b登入Insert = b登入Insert;
            this.b登入Edit = b登入Edit;
            this.b登入Del = b登入Del;
        }

        private void frmLoginMagnt_Load(object sender, EventArgs e)
        {
            if (!b登入Insert)
            {
                btn_add.Enabled = false;
                btn權限新增.Enabled = false;
            }
            if (!b登入Edit)
            {
                btn權限更新.Enabled = false;
                btn_update.Enabled = false;
            }
            if (!b登入Del)
            {
                btn_del.Enabled = false;
                btn權限刪除.Enabled = false;
            }
            Loadall();
            LoalAllRole();
            
        }
        public void Loadall()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {

                con.Open();
                string strcmd = "Select 使用者,姓名,密碼,使用者訊息.使用者權限代號,可行性,權限名稱 from 使用者訊息,使用者權限 where 使用者訊息.使用者權限代號=使用者權限.使用者權限代號";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.Text;
                SqlDataReader data = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                //dt.Columns.Add("項次");//add new 項次 column
                dt.Columns.Add("使用者");
                dt.Columns.Add("姓名");
                dt.Columns.Add("密碼");
                dt.Columns.Add("權限代號");
                dt.Columns.Add("權限名稱");
                dt.Columns.Add("可行性");
                while (data.Read())
                { 
                     DataRow dtrows = dt.NewRow();
                     dtrows["使用者"] = data["使用者"];
                     dtrows["姓名"] = data["姓名"];
                     dtrows["密碼"] = data["密碼"];
                     dtrows["權限代號"] = data["使用者權限代號"];
                     dtrows["權限名稱"] = data["權限名稱"];
                     dtrows["可行性"] =  (bool)data["可行性"] ? "可行" : "不可行";
                     dt.Rows.Add(dtrows);
                }
                dataGVLoginMnt.DataSource = dt.DefaultView;
               
                PublicClass.setRowNumber(dataGVLoginMnt);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }
        private void btn_add_Click(object sender, EventArgs e)
        {
            if (!b登入Insert) btn_add.Enabled = false;
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();

                if (txt使用者.Text == "")
                {
                    MessageBox.Show("您必須輸入使用者", "訊息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txt使用者.Focus();
                    return;
                }
                if (txt密碼.Text == "")
                {
                    MessageBox.Show("您必須輸入密碼", "訊息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txt密碼.Focus();
                    return;
                }
                string sp_insert = "sp_使用者訊息_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@使用者", txt使用者.Text);
                cmd.Parameters.AddWithValue("@姓名", txt姓名.Text);
                cmd.Parameters.AddWithValue("@密碼", PublicClass.md5(txt密碼.Text));
                cmd.Parameters.AddWithValue("@使用者權限代號", txt權限.Text);
                cmd.Parameters.AddWithValue("@可行性", chb可行性.Checked);
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功", "訊息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Loadall();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void clear()
        {
            txt使用者.ResetText();
            //txt使用者.Enabled = true;
            txt姓名.ResetText();
            txt密碼.ResetText();
            txt權限.ResetText();
            txt權限名稱顯示.ResetText();
            chb可行性.Checked = false;

        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (!b登入Del) btn_del.Enabled = false;
            
            DialogResult answer = MessageBox.Show("您確定要刪除這個帳號碼？", "警報", MessageBoxButtons.YesNo);
            if (answer == DialogResult.Yes)
            {
                
                try
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    int r = dataGVLoginMnt.CurrentCell.RowIndex;
                    string user = dataGVLoginMnt.Rows[r].Cells[0].Value.ToString();//txt使用者.Text.Trim();
                    con.Open();
                    string strcmd = string.Format("delete from 使用者訊息 where 使用者='{0}'", user);
                    SqlCommand cmd = new SqlCommand(strcmd, con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("刪除成功","訊息",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    Loadall();
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    
                }
            }
            else return;
           
        }

        private void dataGVLoginMnt_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {

                int r = e.RowIndex;//get current cell index
                txt使用者.Text = dataGVLoginMnt.Rows[r].Cells[0].Value.ToString();
                //txt使用者.Enabled = false;
                txt姓名.Text = dataGVLoginMnt.Rows[r].Cells[1].Value.ToString();
                txt密碼.Text = "";
                oldpassword = dataGVLoginMnt.Rows[r].Cells[2].Value.ToString();
                txt權限.Text=dataGVLoginMnt.Rows[r].Cells[3].Value.ToString();
                txt權限名稱顯示.Text = dataGVLoginMnt.Rows[r].Cells[4].Value.ToString();
                chb可行性.Checked = dataGVLoginMnt.Rows[r].Cells[5].Value.ToString()=="可行"?true:false;
               
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            if (!b登入Edit) btn_update.Enabled = false;
            try
            {
                if (txt使用者.Text == "")
                {
                    MessageBox.Show("您必須輸入使用者", "訊息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txt使用者.Focus();
                    return;
                }
               
                SqlConnection con = new SqlConnection(connectionString);
                string sp_update = "sp_使用者訊息_更新";
                con.Open();
                SqlCommand cmd = new SqlCommand(sp_update, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@使用者", txt使用者.Text);
                cmd.Parameters.AddWithValue("@姓名", txt姓名.Text);
                if (txt密碼.Text != "") cmd.Parameters.AddWithValue("@密碼", PublicClass.md5(txt密碼.Text));
                else cmd.Parameters.AddWithValue("@密碼", oldpassword);
                cmd.Parameters.AddWithValue("@使用者權限代號", txt權限.Text);
                cmd.Parameters.AddWithValue("@可行性", chb可行性.Checked);
                cmd.ExecuteNonQuery();
                MessageBox.Show("更新成功", "訊息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Loadall();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void dataGVLoginMnt_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            
            if (e.ColumnIndex == 2 && e.Value!=null) e.Value = new string('*', e.Value.ToString().Length);
        }

        private void btn權限新增_Click(object sender, EventArgs e)
        {
            //if (!b登入Insert)
            //{
            //    MessageBox.Show("很抱歉，您沒有權限！");
            //    return;
            //}
            if (txt權限代號.Text =="")
            {
                MessageBox.Show("必須填入權限代號！");
                txt權限代號.Focus();
                return;
            }
            if (txt權限名稱.Text=="")
            {
                MessageBox.Show("必須填入權限名稱！");
                txt權限名稱.Focus();
                return;
            }
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                con.Open();
                string strcmd = "sp_使用者權限_新增";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                //int r = dataGridView權限.CurrentCell.RowIndex;
                //string Id = dataGridView權限.Rows[r].Cells[0].Value.ToString();
                //cmd.Parameters.AddWithValue("@使用者權限代號1", Id);
                cmd.Parameters.AddWithValue("@使用者權限代號",txt權限代號.Text);
                cmd.Parameters.AddWithValue("@權限名稱", txt權限名稱.Text);
                cmd.Parameters.AddWithValue("@備註", txt備註.Text);

                cmd.Parameters.AddWithValue("@圖面View", chb圖面View.Checked);
                cmd.Parameters.AddWithValue("@圖面Search", chb圖面Search.Checked);
                cmd.Parameters.AddWithValue("@圖面Insert", chb圖面Insert.Checked);
                cmd.Parameters.AddWithValue("@圖面Edit", chb圖面Edit.Checked);
                cmd.Parameters.AddWithValue("@圖面Del", chb圖面Del.Checked);

                cmd.Parameters.AddWithValue("@文件View", chb文件View.Checked);
                cmd.Parameters.AddWithValue("@文件Search", chb文件Search.Checked);
                cmd.Parameters.AddWithValue("@文件Insert", chb文件Insert.Checked);
                cmd.Parameters.AddWithValue("@文件Edit", chb文件Edit.Checked);
                cmd.Parameters.AddWithValue("@文件Del", chb文件Del.Checked);

                cmd.Parameters.AddWithValue("@庫存View", chb庫存View.Checked);
                cmd.Parameters.AddWithValue("@庫存Search", chb庫存Search.Checked);
                cmd.Parameters.AddWithValue("@庫存Insert", chb庫存Insert.Checked);
                cmd.Parameters.AddWithValue("@庫存Edit", chb庫存Edit.Checked);
                cmd.Parameters.AddWithValue("@庫存Del", chb庫存Del.Checked);

                cmd.Parameters.AddWithValue("@量具View", chb量具View.Checked);
                cmd.Parameters.AddWithValue("@量具Search", chb量具Search.Checked);
                cmd.Parameters.AddWithValue("@量具Insert", chb量具Insert.Checked);
                cmd.Parameters.AddWithValue("@量具Edit", chb量具Edit.Checked);
                cmd.Parameters.AddWithValue("@量具Del", chb量具Del.Checked);

                cmd.Parameters.AddWithValue("@刀具View", chb刀具View.Checked);
                cmd.Parameters.AddWithValue("@刀具Search", chb刀具Search.Checked);
                cmd.Parameters.AddWithValue("@刀具Insert", chb刀具Insert.Checked);
                cmd.Parameters.AddWithValue("@刀具Edit", chb刀具Edit.Checked);
                cmd.Parameters.AddWithValue("@刀具Del", chb刀具Del.Checked);

                cmd.Parameters.AddWithValue("@手工具View", chb手工具View.Checked);
                cmd.Parameters.AddWithValue("@手工具Search", chb手工具Search.Checked);
                cmd.Parameters.AddWithValue("@手工具Insert", chb手工具Insert.Checked);
                cmd.Parameters.AddWithValue("@手工具Edit", chb手工具Edit.Checked);
                cmd.Parameters.AddWithValue("@手工具Del", chb手工具Del.Checked);

                cmd.Parameters.AddWithValue("@NC程式View", chbNC程式View.Checked);
                cmd.Parameters.AddWithValue("@NC程式Search", chbNC程式Search.Checked);
                cmd.Parameters.AddWithValue("@NC程式Insert", chbNC程式Insert.Checked);
                cmd.Parameters.AddWithValue("@NC程式Edit", chbNC程式Edit.Checked);
                cmd.Parameters.AddWithValue("@NC程式Del", chbNC程式Del.Checked);

                cmd.Parameters.AddWithValue("@車輛保養View", chb車輛View.Checked);
                cmd.Parameters.AddWithValue("@車輛保養Search", chb車輛Search.Checked);
                cmd.Parameters.AddWithValue("@車輛保養Insert", chb車輛Insert.Checked);
                cmd.Parameters.AddWithValue("@車輛保養Edit", chb車輛Edit.Checked);
                cmd.Parameters.AddWithValue("@車輛保養Del", chb車輛Del.Checked);

                cmd.Parameters.AddWithValue("@天車View", chb天車View.Checked);
                cmd.Parameters.AddWithValue("@天車Search", chb天車Search.Checked);
                cmd.Parameters.AddWithValue("@天車Insert", chb天車Insert.Checked);
                cmd.Parameters.AddWithValue("@天車Edit", chb天車Edit.Checked);
                cmd.Parameters.AddWithValue("@天車Del", chb天車Del.Checked);

                cmd.Parameters.AddWithValue("@登入View", chb登入View.Checked);
                cmd.Parameters.AddWithValue("@登入Search", chb登入Search.Checked);
                cmd.Parameters.AddWithValue("@登入Insert", chb登入Insert.Checked);
                cmd.Parameters.AddWithValue("@登入Edit", chb登入Edit.Checked);
                cmd.Parameters.AddWithValue("@登入Del", chb登入Del.Checked);

                cmd.ExecuteNonQuery();
                LoalAllRole();
                MessageBox.Show("新增成功！");
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }
        public void LoalAllRole()
        {
             SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string strcmd = "select 使用者權限代號 as 權限代號,權限名稱,備註 from 使用者權限";
                SqlDataAdapter da = new SqlDataAdapter(strcmd, con);
                //da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView權限.DataSource = dt;
                PublicClass.setRowNumber(dataGridView權限);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); con.Close(); }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            clear();
        }

        private void btn權限更新_Click(object sender, EventArgs e)
        {
            //if (!b登入Edit)
            //{
            //    MessageBox.Show("很抱歉，您沒有權限","訊息",MessageBoxButtons.OK);
            //    return;
            //}
            if (txt權限代號.Text == "")
            {
                MessageBox.Show("必須填入權限代號！");
                txt權限代號.Focus();
                return;
            }
            if (txt權限名稱.Text == "")
            {
                MessageBox.Show("必須填入權限名稱！");
                txt權限名稱.Focus();
                return;
            }
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                con.Open();
                string strcmd = "sp_使用者權限_更新";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                int r = dataGridView權限.CurrentCell.RowIndex;
                string Id = dataGridView權限.Rows[r].Cells[0].Value.ToString();
                cmd.Parameters.AddWithValue("@使用者權限代號1", Id);
                cmd.Parameters.AddWithValue("@使用者權限代號", txt權限代號.Text);
                cmd.Parameters.AddWithValue("@權限名稱", txt權限名稱.Text);
                cmd.Parameters.AddWithValue("@備註", txt備註.Text);

                cmd.Parameters.AddWithValue("@圖面View", chb圖面View.Checked);
                cmd.Parameters.AddWithValue("@圖面Search", chb圖面Search.Checked);
                cmd.Parameters.AddWithValue("@圖面Insert", chb圖面Insert.Checked);
                cmd.Parameters.AddWithValue("@圖面Edit", chb圖面Edit.Checked);
                cmd.Parameters.AddWithValue("@圖面Del", chb圖面Del.Checked);

                cmd.Parameters.AddWithValue("@文件View", chb文件View.Checked);
                cmd.Parameters.AddWithValue("@文件Search", chb文件Search.Checked);
                cmd.Parameters.AddWithValue("@文件Insert", chb文件Insert.Checked);
                cmd.Parameters.AddWithValue("@文件Edit", chb文件Edit.Checked);
                cmd.Parameters.AddWithValue("@文件Del", chb文件Del.Checked);

                cmd.Parameters.AddWithValue("@庫存View", chb庫存View.Checked);
                cmd.Parameters.AddWithValue("@庫存Search", chb庫存Search.Checked);
                cmd.Parameters.AddWithValue("@庫存Insert", chb庫存Insert.Checked);
                cmd.Parameters.AddWithValue("@庫存Edit", chb庫存Edit.Checked);
                cmd.Parameters.AddWithValue("@庫存Del", chb庫存Del.Checked);

                cmd.Parameters.AddWithValue("@量具View", chb量具View.Checked);
                cmd.Parameters.AddWithValue("@量具Search", chb量具Search.Checked);
                cmd.Parameters.AddWithValue("@量具Insert", chb量具Insert.Checked);
                cmd.Parameters.AddWithValue("@量具Edit", chb量具Edit.Checked);
                cmd.Parameters.AddWithValue("@量具Del", chb量具Del.Checked);

                cmd.Parameters.AddWithValue("@刀具View", chb刀具View.Checked);
                cmd.Parameters.AddWithValue("@刀具Search", chb刀具Search.Checked);
                cmd.Parameters.AddWithValue("@刀具Insert", chb刀具Insert.Checked);
                cmd.Parameters.AddWithValue("@刀具Edit", chb刀具Edit.Checked);
                cmd.Parameters.AddWithValue("@刀具Del", chb刀具Del.Checked);

                cmd.Parameters.AddWithValue("@手工具View", chb手工具View.Checked);
                cmd.Parameters.AddWithValue("@手工具Search", chb手工具Search.Checked);
                cmd.Parameters.AddWithValue("@手工具Insert", chb手工具Insert.Checked);
                cmd.Parameters.AddWithValue("@手工具Edit", chb手工具Edit.Checked);
                cmd.Parameters.AddWithValue("@手工具Del", chb手工具Del.Checked);

                cmd.Parameters.AddWithValue("@NC程式View", chbNC程式View.Checked);
                cmd.Parameters.AddWithValue("@NC程式Search", chbNC程式Search.Checked);
                cmd.Parameters.AddWithValue("@NC程式Insert", chbNC程式Insert.Checked);
                cmd.Parameters.AddWithValue("@NC程式Edit", chbNC程式Edit.Checked);
                cmd.Parameters.AddWithValue("@NC程式Del", chbNC程式Del.Checked);

                cmd.Parameters.AddWithValue("@車輛保養View", chb車輛View.Checked);
                cmd.Parameters.AddWithValue("@車輛保養Search", chb車輛Search.Checked);
                cmd.Parameters.AddWithValue("@車輛保養Insert", chb車輛Insert.Checked);
                cmd.Parameters.AddWithValue("@車輛保養Edit", chb車輛Edit.Checked);
                cmd.Parameters.AddWithValue("@車輛保養Del", chb車輛Del.Checked);

                cmd.Parameters.AddWithValue("@天車View", chb天車View.Checked);
                cmd.Parameters.AddWithValue("@天車Search", chb天車Search.Checked);
                cmd.Parameters.AddWithValue("@天車Insert", chb天車Insert.Checked);
                cmd.Parameters.AddWithValue("@天車Edit", chb天車Edit.Checked);
                cmd.Parameters.AddWithValue("@天車Del", chb天車Del.Checked);

                cmd.Parameters.AddWithValue("@登入View", chb登入View.Checked);
                cmd.Parameters.AddWithValue("@登入Search", chb登入Search.Checked);
                cmd.Parameters.AddWithValue("@登入Insert", chb登入Insert.Checked);
                cmd.Parameters.AddWithValue("@登入Edit", chb登入Edit.Checked);
                cmd.Parameters.AddWithValue("@登入Del", chb登入Del.Checked);

                cmd.ExecuteNonQuery();
                LoalAllRole();
                MessageBox.Show("新增成功！");
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void dataGridView權限_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            
            SqlConnection con = new SqlConnection(connectionString);
            int r = e.RowIndex;
            string Id=dataGridView權限.Rows[r].Cells["權限代號"].Value.ToString();
            txt權限代號.Text = Id;
            txt權限名稱.Text = dataGridView權限.Rows[r].Cells["權限名稱"].Value.ToString();
            txt備註.Text = dataGridView權限.Rows[r].Cells[2].Value.ToString();

            //chb圖面View.Checked=
            try
            {
                con.Open();
                string strcmd =String.Format("select * from 使用者權限 where 使用者權限代號='{0}'",Id);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    chb圖面View.Checked = (bool)reader["圖面View"];
                    chb圖面Search.Checked = (bool)reader["圖面Search"];
                    chb圖面Insert.Checked = (bool)reader["圖面Insert"];
                    chb圖面Edit.Checked = (bool)reader["圖面Edit"];
                    chb圖面Del.Checked = (bool)reader["圖面Del"];

                    chb文件View.Checked = (bool)reader["文件View"];
                    chb文件Search.Checked = (bool)reader["文件Search"];
                    chb文件Insert.Checked = (bool)reader["文件Insert"];
                    chb文件Edit.Checked = (bool)reader["文件Edit"];
                    chb文件Del.Checked = (bool)reader["文件Del"];

                    chb庫存View.Checked = (bool)reader["庫存View"];
                    chb庫存Search.Checked = (bool)reader["庫存Search"];
                    chb庫存Insert.Checked = (bool)reader["庫存Insert"];
                    chb庫存Edit.Checked = (bool)reader["庫存Edit"];
                    chb庫存Del.Checked = (bool)reader["庫存Del"];

                    chb量具View.Checked = (bool)reader["量具View"];
                    chb量具Search.Checked = (bool)reader["量具Search"];
                    chb量具Insert.Checked = (bool)reader["量具Insert"];
                    chb量具Edit.Checked = (bool)reader["量具Edit"];
                    chb量具Del.Checked = (bool)reader["量具Del"];

                    chb刀具View.Checked = (bool)reader["刀具View"];
                    chb刀具Search.Checked = (bool)reader["刀具Search"];
                    chb刀具Insert.Checked = (bool)reader["刀具Insert"];
                    chb刀具Edit.Checked = (bool)reader["刀具Edit"];
                    chb刀具Del.Checked = (bool)reader["刀具Del"];

                    chb手工具View.Checked = (bool)reader["手工具View"];
                    chb手工具Search.Checked = (bool)reader["手工具Search"];
                    chb手工具Insert.Checked = (bool)reader["手工具Insert"];
                    chb手工具Edit.Checked = (bool)reader["手工具Edit"];
                    chb手工具Del.Checked = (bool)reader["手工具Del"];

                    chbNC程式View.Checked = (bool)reader["NC程式View"];
                    chbNC程式Search.Checked = (bool)reader["NC程式Search"];
                    chbNC程式Insert.Checked = (bool)reader["NC程式Insert"];
                    chbNC程式Edit.Checked = (bool)reader["NC程式Edit"];
                    chbNC程式Del.Checked = (bool)reader["NC程式Del"];

                    chb車輛View.Checked = (bool)reader["車輛保養View"];
                    chb車輛Search.Checked = (bool)reader["車輛保養Search"];
                    chb車輛Insert.Checked = (bool)reader["車輛保養Insert"];
                    chb車輛Edit.Checked = (bool)reader["車輛保養Edit"];
                    chb車輛Del.Checked = (bool)reader["車輛保養Del"];

                    chb天車View.Checked = (bool)reader["天車View"];
                    chb天車Search.Checked = (bool)reader["天車Search"];
                    chb天車Insert.Checked = (bool)reader["天車Insert"];
                    chb天車Edit.Checked = (bool)reader["天車Edit"];
                    chb天車Del.Checked = (bool)reader["天車Del"];

                    chb登入View.Checked = (bool)reader["登入View"];
                    chb登入Search.Checked = (bool)reader["登入Search"];
                    chb登入Insert.Checked = (bool)reader["登入Insert"];
                    chb登入Edit.Checked = (bool)reader["登入Edit"];
                    chb登入Del.Checked = (bool)reader["登入Del"];
                }
                //dataGridView權限.Refresh(); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btn權限整理_Click(object sender, EventArgs e)
        {
            txt權限代號.ResetText();
            txt權限名稱.ResetText();
            txt備註.ResetText();

            chb圖面View.Checked = false;
            chb圖面Search.Checked=false;
            chb圖面Insert.Checked=false;
            chb圖面Edit.Checked=false;
            chb圖面Del.Checked=false;

            chb文件View.Checked=false;
            chb文件Search.Checked=false;
            chb文件Insert.Checked=false;
            chb文件Edit.Checked=false;
            chb文件Del.Checked=false;

            chb庫存View.Checked=false;
            chb庫存Search.Checked=false;
            chb庫存Insert.Checked=false;
            chb庫存Edit.Checked=false;
            chb庫存Del.Checked=false;

            chb量具View.Checked=false;
            chb量具Search.Checked=false;
            chb量具Insert.Checked=false;
            chb量具Edit.Checked=false;
            chb量具Del.Checked=false;

            chb刀具View.Checked=false;
            chb刀具Search.Checked=false;
            chb刀具Insert.Checked=false;
             chb刀具Edit.Checked=false;
            chb刀具Del.Checked=false;

            chb手工具View.Checked=false;
            chb手工具Search.Checked=false;
            chb手工具Insert.Checked=false;
            chb手工具Edit.Checked=false;
            chb手工具Del.Checked=false;

            chbNC程式View.Checked = false;
            chbNC程式Search.Checked=false;
            chbNC程式Insert.Checked=false;
            chbNC程式Edit.Checked=false;
            chbNC程式Del.Checked=false;

            chb車輛View.Checked=false;
            chb車輛Search.Checked=false;
            chb車輛Insert.Checked=false;
            chb車輛Edit.Checked=false;
            chb車輛Del.Checked=false;

            chb天車View.Checked=false;
            chb天車Search.Checked=false;
            chb天車Insert.Checked=false;
            chb天車Edit.Checked=false;
           chb天車Del.Checked=false;

            chb登入View.Checked=false;
            chb登入Search.Checked=false;
            chb登入Insert.Checked=false;
            chb登入Edit.Checked=false;
            chb登入Del.Checked=false;
        }

        private void btn權限刪除_Click(object sender, EventArgs e)
        {
            //if (!b登入Del)
            //{
            //    MessageBox.Show("很抱歉，您沒有權限!");
            //    return;
            //}
            SqlConnection con = new SqlConnection(connectionString);
            int r =dataGridView權限.CurrentCell.RowIndex;
            string Id = dataGridView權限.Rows[r].Cells[0].Value.ToString();
            DialogResult digresult = MessageBox.Show("您確定要刪除這個權限碼？", "通知", MessageBoxButtons.OKCancel);
            if (digresult == DialogResult.OK)
            {
                try
                {
                    con.Open();
                    string sp = "sp_使用者權限_刪除";
                    SqlCommand cmd = new SqlCommand(sp, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@使用者權限代號", Id);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("刪除成功!");
                    LoalAllRole();
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    con.Close();
                }
            }
            else return;
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            frmRoleList frm = new frmRoleList(txt權限, txt權限名稱顯示);
            frm.ShowDialog();
            
        }

        private void dataGVLoginMnt_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGVLoginMnt);
        }

        private void dataGridView權限_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGridView權限);
        }

        private void tab登入_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tab登入.SelectedIndex == 1) PublicClass.setRowNumber(dataGridView權限);
            if (tab登入.SelectedIndex == 0) Loadall();
        }

        private void chb圖面View_CheckStateChanged(object sender, EventArgs e)
        {
           // MessageBox.Show("vv");
        }
        //圖面 checkbox

        private void chb圖面View_Click(object sender, EventArgs e)
        {
            if (chb圖面Search.Checked || chb圖面Insert.Checked || chb圖面Edit.Checked || chb圖面Del.Checked) chb圖面View.Checked = true;
        }

        private void chb圖面Search_Click(object sender, EventArgs e)
        {
            if (chb圖面Search.Checked) chb圖面View.Checked = true;
        }

        private void chb圖面Insert_Click_1(object sender, EventArgs e)
        {
            if (chb圖面Insert.Checked) chb圖面View.Checked = true;
        }

        private void chb圖面Edit_Click(object sender, EventArgs e)
        {
            if (chb圖面Edit.Checked) chb圖面View.Checked = true;
        }

        private void chb圖面Del_Click(object sender, EventArgs e)
        {
            if (chb圖面Del.Checked) chb圖面View.Checked = true;
        }
        //
        //文件 checkbox
        private void chb文件View_Click(object sender, EventArgs e)
        {
            if (chb文件Search.Checked || chb文件Insert.Checked || chb文件Edit.Checked || chb文件Del.Checked) chb文件View.Checked = true;
        }

        private void chb文件Search_Click(object sender, EventArgs e)
        {
            if (chb文件Search.Checked) chb文件View.Checked = true;
        }

        private void chb文件Insert_Click(object sender, EventArgs e)
        {
            if (chb文件Insert.Checked) chb文件View.Checked = true;
        }

        private void chb文件Edit_Click(object sender, EventArgs e)
        {
            if (chb文件Edit.Checked) chb文件View.Checked = true;
        }

        private void chb文件Del_Click(object sender, EventArgs e)
        {
            if (chb文件Del.Checked) chb文件View.Checked = true;
        }

        //庫存 check
        private void chb庫存View_Click(object sender, EventArgs e)
        {
            if (chb庫存Search.Checked || chb庫存Insert.Checked || chb庫存Edit.Checked || chb庫存Del.Checked) chb庫存View.Checked = true;
        }

        private void chb庫存Search_Click(object sender, EventArgs e)
        {
            if (chb庫存Search.Checked) chb庫存View.Checked = true;
        }

        private void chb庫存Insert_Click(object sender, EventArgs e)
        {
            if (chb庫存Insert.Checked) chb庫存View.Checked = true;
        }

        private void chb庫存Edit_Click(object sender, EventArgs e)
        {
            if (chb庫存Edit.Checked) chb庫存View.Checked = true;
        }

        private void chb庫存Del_Click(object sender, EventArgs e)
        {
            if (chb庫存Del.Checked) chb庫存View.Checked = true;
        }
        //量具 checkbox

        private void chb量具View_Click(object sender, EventArgs e)
        {
            if (chb量具Search.Checked || chb量具Insert.Checked || chb量具Edit.Checked || chb量具Del.Checked) chb量具View.Checked = true;
        }

        private void chb量具Search_Click(object sender, EventArgs e)
        {
            if (chb量具Search.Checked) chb量具View.Checked = true;
        }

        private void chb量具Insert_Click(object sender, EventArgs e)
        {
            if (chb量具Insert.Checked) chb量具View.Checked = true;
        }

        private void chb量具Edit_Click(object sender, EventArgs e)
        {
            if (chb量具Edit.Checked) chb量具View.Checked = true;
        }

        private void chb量具Del_Click(object sender, EventArgs e)
        {
            if (chb量具Del.Checked) chb量具View.Checked = true;
        }
        //刀具 checkbox
        private void chb刀具View_Click(object sender, EventArgs e)
        {
            if (chb刀具Search.Checked || chb刀具Insert.Checked || chb刀具Edit.Checked || chb刀具Del.Checked) chb刀具View.Checked = true;
        }

        private void chb刀具Search_Click(object sender, EventArgs e)
        {
            if (chb刀具Search.Checked) chb刀具View.Checked = true;
        }

        private void chb刀具Insert_Click(object sender, EventArgs e)
        {
            if (chb刀具Insert.Checked) chb刀具View.Checked = true;
        }

        private void chb刀具Edit_Click(object sender, EventArgs e)
        {
            if (chb刀具Edit.Checked) chb刀具View.Checked = true;
        }

        private void chb刀具Del_Click(object sender, EventArgs e)
        {
            if (chb刀具Del.Checked) chb刀具View.Checked = true;
        }
        //
        //手工具 checkbox
        private void chb手工具View_Click(object sender, EventArgs e)
        {
            if (chb手工具Search.Checked || chb手工具Insert.Checked || chb手工具Edit.Checked || chb手工具Del.Checked) chb手工具View.Checked = true;
        }

        private void chb手工具Search_Click(object sender, EventArgs e)
        {
            if (chb手工具Search.Checked) chb手工具View.Checked = true;
        }

        private void chb手工具Insert_Click(object sender, EventArgs e)
        {
            if (chb手工具Insert.Checked) chb手工具View.Checked = true;
        }

        private void chb手工具Edit_Click(object sender, EventArgs e)
        {
            if (chb手工具Edit.Checked) chb手工具View.Checked = true;
        }

        private void chb手工具Del_Click(object sender, EventArgs e)
        {
            if (chb手工具Del.Checked) chb手工具View.Checked = true;
        }
        //
        //NC程式 checkbox
        private void chbNC程式View_Click(object sender, EventArgs e)
        {
            if (chbNC程式Search.Checked || chbNC程式Insert.Checked || chbNC程式Edit.Checked || chbNC程式Del.Checked) chbNC程式View.Checked = true;
        }

        private void chbNC程式Search_Click(object sender, EventArgs e)
        {
            if (chbNC程式Search.Checked) chbNC程式View.Checked = true;
        }

        private void chbNC程式Insert_Click(object sender, EventArgs e)
        {
            if (chbNC程式Insert.Checked) chbNC程式View.Checked = true;
        }

        private void chbNC程式Edit_Click(object sender, EventArgs e)
        {
            if (chbNC程式Edit.Checked) chbNC程式View.Checked = true;
        }

        private void chbNC程式Del_Click(object sender, EventArgs e)
        {
            if (chbNC程式Del.Checked) chbNC程式View.Checked = true;
        }
        //
        //車輛 checkbox
        private void chb車輛View_Click(object sender, EventArgs e)
        {
            if (chb車輛Search.Checked || chb車輛Insert.Checked || chb車輛Edit.Checked || chb車輛Del.Checked) chb車輛View.Checked = true;
        }

        private void chb車輛Search_Click(object sender, EventArgs e)
        {
            if (chb車輛Search.Checked) chb車輛View.Checked = true;
        }

        private void chb車輛Insert_Click(object sender, EventArgs e)
        {
            if (chb車輛Insert.Checked) chb車輛View.Checked = true;
        }

        private void chb車輛Edit_Click(object sender, EventArgs e)
        {
            if (chb車輛Edit.Checked) chb車輛View.Checked = true;
        }

        private void chb車輛Del_Click(object sender, EventArgs e)
        {
            if (chb車輛Del.Checked) chb車輛View.Checked = true;
        }
        //
        //天車 checkbox
        private void chb天車View_Click(object sender, EventArgs e)
        {
            if (chb天車Search.Checked || chb天車Insert.Checked || chb天車Edit.Checked || chb天車Del.Checked) chb天車View.Checked = true;
        }

        private void chb天車Search_Click(object sender, EventArgs e)
        {
            if (chb天車Search.Checked) chb天車View.Checked = true;
        }

        private void chb天車Insert_Click(object sender, EventArgs e)
        {
            if (chb天車Insert.Checked) chb天車View.Checked = true;
        }

        private void chb天車Edit_Click(object sender, EventArgs e)
        {
            if (chb天車Edit.Checked) chb天車View.Checked = true;
        }

        private void chb天車Del_Click(object sender, EventArgs e)
        {
            if (chb天車Del.Checked) chb天車View.Checked = true;
        }
        //
        //登入 checkbox
        private void chb登入View_Click(object sender, EventArgs e)
        {
            if (chb登入Search.Checked || chb登入Insert.Checked || chb登入Edit.Checked || chb登入Del.Checked) chb登入View.Checked = true;
        }

        private void chb登入Search_Click(object sender, EventArgs e)
        {
            if (chb登入Search.Checked) chb登入View.Checked = true;
        }

        private void chb登入Insert_Click(object sender, EventArgs e)
        {
            if (chb登入Insert.Checked) chb登入View.Checked = true;
        }

        private void chb登入Edit_Click(object sender, EventArgs e)
        {
            if (chb登入Edit.Checked) chb登入View.Checked = true;
        }

        private void chb登入Del_Click(object sender, EventArgs e)
        {
            if (chb登入Del.Checked) chb登入View.Checked = true;
        }
    }
}
